unit uDataCenterApi;

interface

uses SysUtils, Classes, Windows;

function InitServerSide(const pszConnectKcbpTxt: PAnsiChar; const pszConFileName: PAnsiChar ): bool; cdecl; external 'Kernel.DLL';

function GetSeparationCount(pStr, pSeparationStr: AnsiString):Integer;
function GetStrCount(pStr, pSubStr: AnsiString): Integer;
function GetSeparationStr(pStr, pSeparationStr: AnsiString; pOrdId: Integer; pDirection: AnsiString = 'L'): AnsiString;
function GetLeftStr(pStr: AnsiString; pCount: Integer): AnsiString;
function GetRightStr(pStr: AnsiString; pCount: Integer): AnsiString;
function GetCommandParaValue(pStr, pCommandOpt: AnsiString): AnsiString;

implementation

function GetSeparationCount(pStr, pSeparationStr: AnsiString):Integer;
var ASeparationStrLen: Integer;
begin
  ASeparationStrLen := Length(pSeparationStr);
  Result := GetStrCount(pStr, pSeparationStr);
  if (Result > 0) and
     (GetRightStr(pStr,ASeparationStrLen) <> pSeparationStr) then
    Inc(Result);
end;

function GetStrCount(pStr, pSubStr: AnsiString): Integer;
var ASubStrLen, ASum, APos: Integer;
begin
  ASum := 0;
  ASubStrLen := Length(pSubStr);
  while Pos(pSubStr, pStr)<>0 do
    begin
      APos := Pos(pSubStr, pStr);
      pStr := Copy(pStr, APos + ASubStrLen, Length(pStr)- APos - ASubStrLen + 1);
      Inc(ASum);
    end;
  Result := ASum;
end;

function GetSeparationStr(pStr, pSeparationStr: AnsiString; pOrdId: Integer; pDirection: AnsiString = 'L'): AnsiString;
var ASeparationStrLen,i: Integer;
begin
  Result := '';
  pDirection := UpperCase(pDirection);
  ASeparationStrLen := Length(pSeparationStr);
  if Pos(pDirection,'L,R') = 0 then
    pDirection:='L';
  if GetRightStr(pStr,ASeparationStrLen) <> pSeparationStr then
    pStr := pStr + pSeparationStr;
  if pDirection = 'R' then
    Inc(pOrdId);
  i := 1;
  while Pos(pSeparationStr,pStr)<>0 do
    begin
      if i = pOrdId then
        begin
          Result := Copy(pStr,1,Pos(pSeparationStr, pStr)-1);
          Break;
        end;
      pStr := Copy(pStr, Pos(pSeparationStr, pStr)+ASeparationStrLen, Length(pStr)-Pos(pSeparationStr, pStr)-ASeparationStrLen+1);
      Inc(i);
    end;
end;

function GetRightStr(pStr: AnsiString; pCount: Integer): AnsiString;
begin
  Result := Copy( pStr, Length(pStr) - pCount + 1, pCount );
end;

function GetCommandParaValue(pStr, pCommandOpt: AnsiString): AnsiString;
var i, ACount: Integer;
    ACommandOpt: AnsiString;
    ATmp: AnsiString;
begin
  Result := '';
  ACount := GetSeparationCount(pStr, '-');
  i := 2;
  for i := 2 to ACount do
    begin
      ATmp := TrimLeft(GetSeparationStr(pStr, '-', i));
      ACommandOpt := '-' + GetLeftStr(ATmp, Length(pCommandOpt) - 1);
      if LowerCase(ACommandOpt) = LowerCase(pCommandOpt) then
        begin
          Result := Trim(GetRightStr(ATmp, Length(ATmp) - Length(pCommandOpt) + 1));
          Break;
        end;
    end;
end;

function GetLeftStr(pStr: AnsiString; pCount: Integer): AnsiString;
begin
  Result := Copy(pStr,1,pCount);
end;

end.
